package com.leetcode.LC;

import java.util.ArrayList;
import java.util.List;

public class LC507 {
    public boolean checkPerfectNumber(int num) {

        int sum = 0;
        for (Integer integer : positiveFactor(num)) {
            sum += integer;
        }
        if (sum == num) {
            return true;
        } else {
            return false;
        }

    }

    public List<Integer> positiveFactor(int num) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <=num/2 ; i++) {

            if (num % 2 != 0) {
                if (i % 2 == 0) {
                    continue;
                }
            } else if (num % 3 != 0) {
                if (i % 3 == 0) {
                    continue;
                }
            } else if (num % 5 != 0) {
                if (i % 5 == 0) {
                    continue;
                }
            } else if (num % 7 != 0) {
                if (i % 7 == 0) {
                    continue;
                }
            } else if (num % 2 == 0) {
                if (i % 2 == 0) {
                    list.add(i);
                    continue;
                }
            } else if (num % 3 == 0) {
                if (i % 3 == 0) {
                    list.add(i);
                    continue;
                }
            } else if (num % 5 == 0) {
                if (i % 5 == 0) {
                    list.add(i);
                    continue;
                }
            } else if (num % 7 == 0) {
                if (i % 7 == 0) {
                    list.add(i);
                    continue;
                }
            }

            if (num % i == 0) {
                list.add(i);
            }
        }
        return list;
    }

    public boolean checkPerfectNumber2(int num) {
        if(num == 1)return false;
        int factorsSum = 1;
        int n = (int)Math.sqrt(num);
        for(int i = 2; i <= n; i++){
            if(num % i == 0){
                factorsSum += (i + num/i);
            }
        }
        return factorsSum == num;
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(new LC507().checkPerfectNumber2(8128));
        long l2 = System.currentTimeMillis();
        System.out.println(l2 - l);
    }
}
